USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_RUT_EMPRESAS]    Script Date: 11/24/2011 14:35:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_M0002_RUT_EMPRESAS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0002_RUT_EMPRESAS]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0002_RUT_EMPRESAS]    Script Date: 11/24/2011 14:35:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_M0002_RUT_EMPRESAS]
AS
BEGIN
	SELECT	cre.aosuc AS suc
			,cre.aocta AS cta
			,cre.aooper AS oper
			,CAST(Z.Z07327COR AS VARCHAR) + CAST(z.Z07327MAT AS VARCHAR) AS CRRUTEMPRE
	INTO	rut_emp       
	FROM	fsd010 AS cre
			,fsr011 AS ctaemp
			,fsr008 AS rel
			,z07327 AS z
	WHERE	cre.aosbop = 0
	AND		cre.aooper <> 999999999
	AND		cre.aosuc = ctaemp.r2suc 
	AND		cre.aocta = ctaemp.r2cta 
	AND		cre.aooper = r2oper 
	AND		r2sbop = 0 
	AND		ctaemp.relcod = 505 
	AND		cre.aooper = ctaemp.r2oper 
	AND		ctaemp.r1cta = rel.ctnro 
	AND		ctaemp.r1cta = z.z07327cta
	AND		z07327EST = 'AC'
	AND		cre.aotasa <> 0
	GROUP BY cre.aosuc, cre.aocta, cre.aooper, Z.Z07327COR, z.Z07327MAT 
END

GO


